﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using System.Web.UI;
using System.Xml.Linq;

namespace ShopB2C.Helper {
    public class SiteMapPathHelper : Control {
        private HtmlHelper _html;
        private RouteData _rd;
        private HtmlTextWriter _writer;

        public SiteMapPathHelper(HtmlHelper html) {
            this._html = html;
        }

        protected override void Render(HtmlTextWriter writer) {
            base.Render(writer);
            RouteData rd = this._html.ViewContext.RouteData;
            XDocument xd = XDocument.Load(HttpContext.Current.Server.MapPath("~/Config/SiteMapPath.config"));

            var s = xd.Root.Descendants().Where(x => {
                bool c = false, a = false, p = false;
                if (x.Attribute(XName.Get("Controller")) != null) {
                    if (rd.Values.ContainsKey("controller")) {
                        c = x.Attribute(XName.Get("Controller")).Value == rd.Values["controller"].ToString();
                    }
                }
                if (x.Attribute(XName.Get("Action")) != null) {
                    if (rd.Values.ContainsKey("action")) {
                        a = x.Attribute(XName.Get("Action")).Value == rd.Values["action"].ToString();
                    }
                }

                if (x.Attribute(XName.Get("DynamicPara")) != null) {
                    if (!string.IsNullOrEmpty(x.Attribute(XName.Get("DynamicPara")).Value)) {
                        if (rd.Values.ContainsKey(x.Attribute(XName.Get("DynamicPara")).Value)) {
                            p = true;
                        }
                        else { if (c && a) { p = true; } }
                    }

                }
                return c && a && p;
            });

            this._writer = writer;
            this._rd = rd;
            this.WriterHtml(s);
        }

        private void WriterHtml(IEnumerable<XElement> xeList) {
            if (xeList.Count() > 0) {
                this.CreateLink(xeList.First().Parent);
                this._writer.Write(xeList.First().Attribute(XName.Get("Title")).Value);
            }
        }

        private void CreateLink(XElement xe) {
            if (xe != null) {
                if (xe.Parent != null && xe.Parent.HasAttributes) {
                    this.CreateLink(xe.Parent);
                }
                string c = xe.Attribute(XName.Get("Controller")).Value;
                string a = xe.Attribute(XName.Get("Action")).Value;
                string t = xe.Attribute(XName.Get("Title")).Value;
                RouteValueDictionary rvd = null;
                XAttribute xa = xe.Attribute(XName.Get("DynamicPara"));
                if (xa != null) {
                    rvd = new RouteValueDictionary();
                    foreach (string s in xa.Value.Split(',')) {
                        if (this._rd.Values[s] != null) {
                            string v = this._rd.Values[s].ToString();
                            if (!string.IsNullOrEmpty(v)) { rvd.Add(s, v); }
                        }
                    }
                }

                this._writer.Write(this._html.ActionLink(t, a, c, rvd, null).ToHtmlString() + " > ");
            }
        }

        public virtual string CreateHtml() {
            string html = string.Empty;
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.Render(hw);
            hw.Flush();
            hw.Close();
            html = sw.ToString();
            sw.Flush();
            sw.Close();
            return html;
        }
    }

    //public static class MvcSiteMapPathHelper {
    //    public static string SiteMapPath(this HtmlHelper html) {
    //        SiteMapPathHelper siteMapPathHelper = new SiteMapPathHelper(html);
    //        return siteMapPathHelper.CreateHtml();
    //    }
    //}
}
